<?php


namespace app\common\service\companyIndustry\tsinghua;

use app\common\service\BaseService;
use app\common\service\ControlService;
use app\common\service\EmissionService;
use app\common\service\FormulaService;

/**
 * 清华方案 脱硝排放量
 */
class TsinghuaEmissionService extends BaseService
{
    /**
     * （方案值）排放量
     * @@param $companyId
     * @param $drainId
     * @param $facilityId
     * @return int
     */
    public function singleCompanySingleDrainSingleFacility($planNumber, $tsinghuaName, $companyId, $drainId, $facilityId)
    {
        return 1;
    }

    /**
     * 获取清华方案的排放量
     * @param int $companyId 公司ID
     * @param string $planNumber 预案编号
     * @param string $tsinghuaPlanName 清华方案名称
     * @param string $type 污染称类型
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-07-16
     */
    public function getTsinghuaPlanEmission($companyId, $planNumber, $tsinghuaPlanName, $type)
    {
        $data = $this->getTsinghuaSingleCompanyDrainEmission($type, 1, $planNumber, $companyId, $tsinghuaPlanName, $emissionUnit = 'mg');
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 清华数据接口转换获取单位
     * +----------------------------------------------------------
     * @param string $unit
     * +----------------------------------------------------------
     * @return int
     * @author hongwei 2019-07-01
     * +----------------------------------------------------------
     */
    public function getTsinghuaUnit($unit = 'kg')
    {
        $emissionFunction = 0;
        if ($unit == 'mg') {
            $emissionFunction = 1000000;
        }
        if ($unit == 'g') {
            $emissionFunction = 1000;
        }
        if ($unit == 'kg') {
            $emissionFunction = 1;
        }
        if ($unit == 't') {
            $emissionFunction = 0.001;
        }
        return $emissionFunction;
    }

    /**
     * +----------------------------------------------------------
     * 6.1 所有公司 - 所有排口 - 排放量
     * 清华排口方案排放量
     * +----------------------------------------------------------
     * @param string $type 脱硫或脱硝
     * @param $controlModeId
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @param string $emissionUnit 清华默认是 kg
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-06-29
     * +----------------------------------------------------------
     */
    public function getTsinghuaAllCompanyDrainEmissionType($type, $controlModeId, $planNumber, $tsinghuaPlanName, $emissionUnit = 'kg')
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_tsp_emission) as tsinghua_emission';
        }
        $sql  = 'select ' . $field . ' from control_plan_from_tsinghua_drain_details where control_mode_id = ' . $controlModeId . ' and plan_number = "' . $planNumber . '" and tsinghua_plan_name = "' . $tsinghuaPlanName . '" group by drain_id order by drain_id asc';
        $data = $this->pdoFetchAll($sql);

        if (!$data) {
            exception('获取清华今天预案的数据为空，请联系相关技术人员！');
        }
        //增加清华所要的排放量单位
        foreach ($data as $k => $v) {
            $data[$k]['tsinghua_emission']      = $v['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
            $data[$k]['tsinghua_emission_unit'] = $emissionUnit;
        }
        array_multisort(array_column($data, 'drain_id'), SORT_ASC, $data); //排口进行排序
        $data = field_as_key($data, 'drain_id');
        return $data;
    }

    /**
     * 获取清华所有排口的排放量
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @param string $emissionUnit
     * @return array
     * @throws \Exception
     */
    public function getTsinghuaAllCompanyDrainEmission($planNumber, $tsinghuaPlanName, $emissionUnit = 'kg')
    {
        //step1:工业
        $so2Emission = $this->getTsinghuaAllCompanyDrainEmissionType('SO2', 1, $planNumber, $tsinghuaPlanName, $emissionUnit);
        $no2Emission = $this->getTsinghuaAllCompanyDrainEmissionType('NO2', 1, $planNumber, $tsinghuaPlanName, $emissionUnit);
        $tspEmission = $this->getTsinghuaAllCompanyDrainEmissionType('TSP', 1, $planNumber, $tsinghuaPlanName, $emissionUnit);

        //step2:更改字段
        //so2
        foreach ($so2Emission as $k => $v) {
            $so2Emission[$k]['tsinghua_emission_so2'] = $v['tsinghua_emission'];
            unset($so2Emission[$k]['tsinghua_emission']);
        }
        foreach ($no2Emission as $k => $v) {
            $no2Emission[$k]['tsinghua_emission_no2'] = $v['tsinghua_emission'];
            unset($no2Emission[$k]['tsinghua_emission']);
        }
        foreach ($tspEmission as $k => $v) {
            $tspEmission[$k]['tsinghua_emission_tsp'] = $v['tsinghua_emission'];
            unset($tspEmission[$k]['tsinghua_emission']);
        }

        //step2:数组拼接
        $newData = [];
        //step2-1:so2与no2拼接
        foreach ($so2Emission as $k => $v) {
            foreach ($no2Emission as $k2 => $v2) {
                if ($k == $k2) {
                    $newData[$k] = array_merge($v, $v2);
                }
            }
        }
        //step2-2:与tsp拼接
        $newData2 = [];
        foreach ($newData as $k3 => $v3) {
            foreach ($tspEmission as $k4 => $v4) {
                if ($k3 == $k4) {
                    $newData2[$k3] = array_merge($v3, $v4);
                }
            }
        }

        //step2-3:只获取想要的数据
        $newData3 = [];
        foreach ($newData2 as $k5 => $v5) {
            $newData3[$k5]['company_id']            = $v5['company_id'];
            $newData3[$k5]['company_name']          = $v5['company_name'];
            $newData3[$k5]['drain_id']              = $v5['drain_id'];
            $newData3[$k5]['drain_name']            = $v5['drain_name'];
            $newData3[$k5]['tsinghua_emission_so2'] = $v5['tsinghua_emission_so2'];
            $newData3[$k5]['tsinghua_emission_no2'] = $v5['tsinghua_emission_no2'];
            $newData3[$k5]['tsinghua_emission_tsp'] = $v5['tsinghua_emission_tsp'];
            $newData3[$k5]['tsinghua_unit']         = $v5['tsinghua_emission_unit'];
        }
        //去掉以drain_id的键值
        $newData4 = array_values($newData3);
        //返回
        return $newData4;
    }

    /**
     * +----------------------------------------------------------
     * 6.2 所有公司 - 排放量
     * 清华排口方案排放量
     * +----------------------------------------------------------
     * @param string $type 脱硫或脱硝
     * @param $controlModeId
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @param string $emissionUnit 清华默认是 kg
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-06-29
     * +----------------------------------------------------------
     */
    public function getTsinghuaAllCompanyEmissionType($type, $controlModeId, $planNumber, $tsinghuaPlanName, $emissionUnit = 'kg')
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'id,company_id,company_name,sum(company_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'id,company_id,company_name,sum(company_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'id,company_id,company_name,sum(company_tsp_emission) as tsinghua_emission';
        }
        $sql = 'select ' . $field . ' from control_plan_from_tsinghua_company_details where control_mode_id = ' . $controlModeId . ' and plan_number = "' . $planNumber . '" and tsinghua_plan_name = "' . $tsinghuaPlanName . '" group by company_id order by company_id asc';

        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();

        if (!$data) {
            exception('获取清华脱硫和脱硝减排量小时数据失败！');
        }
        //增加清华所要的排放量单位
        foreach ($data as $k => $v) {
            $data[$k]['tsinghua_emission']      = $v['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
            $data[$k]['tsinghua_emission_unit'] = $emissionUnit;
        }
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 6.3 单个公司 - 排放量
     * 清华排口方案排放量
     * +----------------------------------------------------------
     * @param string $companyId 公司ID
     * @param string $type 脱硫或脱硝
     * @param integer $controlModeId 管控方式ID
     * @param string $planNumber 预案编号
     * @param string $tsinghuaPlanName 方案名称
     * @param string $emissionUnit 清华默认是 kg
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-06-29
     * +----------------------------------------------------------
     */
    public function getTsinghuaSingleCompanyEmissionType($companyId, $type, $controlModeId, $planNumber, $tsinghuaPlanName, $emissionUnit = 'kg')
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'id,company_id,company_name,sum(company_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'id,company_id,company_name,sum(company_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'id,company_id,company_name,sum(company_tsp_emission) as tsinghua_emission';
        }
        $sql = 'select ' . $field . ' from control_plan_from_tsinghua_company_details where control_mode_id = ' . $controlModeId . ' and plan_number = "' . $planNumber . '" and tsinghua_plan_name = "' . $tsinghuaPlanName . '" and company_id = ' . (int)$companyId . ' group by company_id order by company_id asc';

        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();

        if (!$data) {
            exception('获取清华脱硫和脱硝减排量小时数据失败！');
        }
        //增加清华所要的排放量单位
        foreach ($data as $k => $v) {
            $data[$k]['tsinghua_emission']      = $v['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
            $data[$k]['tsinghua_emission_unit'] = $emissionUnit;
        }
        return $data[0];
    }

    /**
     * +----------------------------------------------------------
     * 6.4 单个公司 - 所有排口 - 排放量
     * 清华排口方案排放量
     * +----------------------------------------------------------
     * @param string $type 脱硫或脱硝
     * @param $controlModeId
     * @param $planNumber
     * @param $companyId
     * @param $tsinghuaPlanName
     * @param string $emissionUnit 清华默认是 kg
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-06-29
     * +----------------------------------------------------------
     */
    public function getTsinghuaSingleCompanyDrainEmission($type, $controlModeId, $planNumber, $companyId, $tsinghuaPlanName, $emissionUnit = 'kg')
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_tsp_emission) as tsinghua_emission';
        }
        $sql = 'select ' . $field . ' from control_plan_from_tsinghua_drain_details where control_mode_id = ' . $controlModeId;
        $sql .= ' and plan_number = "' . $planNumber . '" and tsinghua_plan_name = "' . $tsinghuaPlanName . '" and company_id = ' . (int)$companyId;
        $sql .= ' order by drain_id asc';
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();

        if (!$data) {
            exception('获取清华脱硫和脱硝减排量小时数据失败！');
        }
        //增加清华所要的排放量单位
        foreach ($data as $k => $v) {
            $data[$k]['tsinghua_emission']      = $v['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
            $data[$k]['tsinghua_emission_unit'] = $emissionUnit;
        }
        array_multisort(array_column($data, 'drain_id'), SORT_ASC, $data); //排口进行排序
        $data = field_as_key($data, 'drain_id');
        return $data;
    }

    /**
     * +----------------------------------------------------------
     * 6.4 单个公司 - 单个排口 - 排放量
     * 清华排口方案排放量
     * +----------------------------------------------------------
     * @param string $type 脱硫或脱硝
     * @param $controlModeId
     * @param $planNumber
     * @param $companyId
     * @param $tsinghuaPlanName
     * @param $every
     * @param string $emissionUnit 清华默认是 kg
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-06-29
     * +----------------------------------------------------------
     */
    public function getTsinghuaSingleCompanyAllDrainsEmission($type, $controlModeId, $planNumber, $companyId, $tsinghuaPlanName, $emissionUnit = 'kg', $every = 'hour')
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'id,company_id,company_name,drain_id,drain_name,sum(drain_tsp_emission) as tsinghua_emission';
        }
        $sql = 'select ' . $field . ' from control_plan_from_tsinghua_drain_details where control_mode_id = ' . $controlModeId;
        $sql .= ' and plan_number = "' . $planNumber . '" and tsinghua_plan_name = "' . $tsinghuaPlanName . '" and company_id = ' . (int)$companyId;
        $sql .= ' order by drain_id asc';
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetch();

        if (!$data) {
            exception('获取清华脱硫和脱硝减排量小时数据失败！');
        }
        //增加清华所要的排放量单位
        $dateTemp['id']                     = $data['id'];
        $dateTemp['company_id']             = $data['company_id'];
        $dateTemp['company_name']           = $data['company_name'];
        $dateTemp['drain_id']               = $data['drain_id'];
        $dateTemp['drain_name']             = $data['drain_name'];
        $dateTemp['tsinghua_emission']      = $data['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
        $dateTemp['tsinghua_emission_unit'] = $emissionUnit;
        //对每天进行排序
        $newData   = [];
        $control   = new ControlService();
        if ($every == 'hour') {
            $everyHour = $control->getControlEveryHour($planNumber);
            foreach ($everyHour as $k => $v) {
                $newData[$k]['tsinghua_emission']      = $v['publish_time'];
                $newData[$k]['emsissions']             = $dateTemp['tsinghua_emission'];
                $newData[$k]['tsinghua_emission_unit'] = $dateTemp['tsinghua_emission_unit'];
            }
        }
        if ($every == 'day') {
            $everyDay = $control->getControlEveryDay($planNumber);
            foreach ($everyDay as $k => $v) {
                $newData[$k]['tsinghua_emission']      = $v['publish_time'];
                $newData[$k]['emsissions']             = $dateTemp['tsinghua_emission'];
                $newData[$k]['tsinghua_emission_unit'] = $dateTemp['tsinghua_emission_unit'];
            }
        }
        return $newData;
    }

    /**
     * 获取清华单个公司单个排口的排放量
     * @author hongwei 2019-08-14
     * @param $type
     * @param $controlModeId
     * @param $planNumber
     * @param $drainId
     * @param $tsinghuaPlanName
     * @param string $emissionUnit
     * @param bool $every
     * @return array
     * @throws \Exception
     */
    public function getTsinghuaSingleCompanySingleDrainEmission($type, $controlModeId, $planNumber, $drainId, $tsinghuaPlanName, $emissionUnit = 'kg', $every = false)
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'drain_id,drain_name,sum(drain_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'drain_id,drain_name,sum(drain_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'drain_id,drain_name,sum(drain_tsp_emission) as tsinghua_emission';
        }
        $sql = 'select ' . $field . ' from control_plan_from_tsinghua_drain_details where control_mode_id = ' . $controlModeId;
        $sql .= ' and plan_number = "' . $planNumber . '" and tsinghua_plan_name = "' . $tsinghuaPlanName . '" and drain_id = ' . (int)$drainId;
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetch();

        if (!$data) {
            exception('获取清华脱硫和脱硝减排量小时数据失败！');
        }
        //增加清华所要的排放量单位
        $dateTemp['drain_id']               = $data['drain_id'];
        $dateTemp['drain_name']             = $data['drain_name'];
        $dateTemp['tsinghua_emission']      = $data['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
        $dateTemp['tsinghua_emission_unit'] = $emissionUnit;
        //对每天进行排序
        $newData   = [];
        $control   = new ControlService();
        if ($every == 'hour') {
            $everyHour = $control->getControlEveryHour($planNumber);
            foreach ($everyHour as $k => $v) {
                $newData[$k]['drain_id']  = $drainId;
                $newData[$k]['time']      = $v['publish_time'];
                if (strtoupper($type) == 'SO2') {
                    $newData[$k]['so2_emission'] = $dateTemp['tsinghua_emission'];
                }
                if (strtoupper($type) == 'NO2') {
                    $newData[$k]['no2_emission'] = $dateTemp['tsinghua_emission'];
                }
            }
            return $newData;
        } else if ($every == 'day') {
            $everyDay = $control->getControlEveryDay($planNumber);
            foreach ($everyDay as $k => $v) {
                $newData[$k]['drain_id']   = $drainId;
                $newData[$k]['time']       = $v['publish_time'];
                if (strtoupper($type) == 'SO2') {
                    $newData[$k]['so2_emission'] = $dateTemp['tsinghua_emission'];
                }
                if (strtoupper($type) == 'NO2') {
                    $newData[$k]['no2_emission'] = $dateTemp['tsinghua_emission'];
                }
            }
            return $newData;
        } else {
            return $dateTemp;
        }
    }

    /**
     * 获取清华方案的排放总量
     * @param $planNumber
     * @param $emissionUnit
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-16
     */
    public function getTsinghuaPlanTotalEmissionHourAvg($planNumber, $emissionUnit = 'kg')
    {
        //step1:获取数据
        $so2Emission = $this->getTsinghuaPlanTotalEmissionType('so2', 1, $planNumber, $emissionUnit);
        $no2Emission = $this->getTsinghuaPlanTotalEmissionType('no2', 1, $planNumber, $emissionUnit);
        $tspEmission = $this->getTsinghuaPlanTotalEmissionType('tsp', 1, $planNumber, $emissionUnit);

        //step1:获取管控开始和结束时间
        $control = new ControlService();
        $endTime = $control->getControlEndTime($planNumber);

        //step2:更改字段
        //so2
        foreach ($so2Emission as $k => $v) {
            $so2Emission[$k]['tsinghua_emission_so2'] = $v['tsinghua_emission'];
            unset($so2Emission[$k]['tsinghua_emission']);
        }
        foreach ($no2Emission as $k => $v) {
            $no2Emission[$k]['tsinghua_emission_no2'] = $v['tsinghua_emission'];
            unset($no2Emission[$k]['tsinghua_emission']);
        }
        foreach ($tspEmission as $k => $v) {
            $tspEmission[$k]['tsinghua_emission_tsp'] = $v['tsinghua_emission'];
            unset($tspEmission[$k]['tsinghua_emission']);
        }

        //step2:数组拼接
        $newData = [];
        //step2-1:so2与no2拼接
        foreach ($so2Emission as $k => $v) {
            foreach ($no2Emission as $k2 => $v2) {
                if ($k == $k2) {
                    $newData[$k] = array_merge($v, $v2);
                }
            }
        }
        //step2-2:与tsp拼接
        $newData2 = [];
        foreach ($newData as $k3 => $v3) {
            foreach ($tspEmission as $k4 => $v4) {
                if ($k3 == $k4) {
                    $newData2[$k3] = array_merge($v3, $v4);
                }
            }
        }

        //step2-3:只获取想要的数据
        $newData3 = [];
        foreach ($newData2 as $k5 => $v5) {
            $newData3[$k5]['tsinghua_plan_id']      = $v5['tsinghua_plan_id'];
            $newData3[$k5]['tsinghua_plan_name']    = $v5['tsinghua_plan_name'];
            $newData3[$k5]['tsinghua_emission_so2'] = $v5['tsinghua_emission_so2'];
            $newData3[$k5]['tsinghua_emission_no2'] = $v5['tsinghua_emission_no2'];
            $newData3[$k5]['tsinghua_emission_tsp'] = $v5['tsinghua_emission_tsp'];
            $newData3[$k5]['tsinghua_unit']         = $v5['tsinghua_emission_unit'];
        }
        //去掉以tsinghua_plan_id的键值
        $newData4 = array_values($newData3);
        //返回
        return $newData4;
    }


    /**
     * 获取清华方案总的排放量
     * @param $type
     * @param $controlModeId
     * @param $planNumber
     * @param $emissionUnit
     * @return array
     * @throws \Exception
     * @author hongwei 2019-07-16
     */
    public function getTsinghuaPlanTotalEmissionType($type, $controlModeId, $planNumber, $emissionUnit)
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'tsinghua_plan_id, tsinghua_plan_name, sum(drain_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'tsinghua_plan_id, tsinghua_plan_name, sum(drain_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'tsinghua_plan_id, tsinghua_plan_name, sum(drain_tsp_emission) as tsinghua_emission';
        }
        $sql = 'select ' . $field . ' from control_plan_from_tsinghua_drain_details where control_mode_id = ' . $controlModeId . ' and plan_number = "' . $planNumber . '" group by tsinghua_plan_name order by tsinghua_plan_id asc';

        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();
        $data = field_as_key($data, 'tsinghua_plan_id');

        if (!$data) {
            exception('获取清华脱硫和脱硝减排量小时数据失败！');
        }
        //增加清华所要的排放量单位
        foreach ($data as $k => $v) {
            $data[$k]['tsinghua_emission']      = $v['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
            $data[$k]['tsinghua_emission_unit'] = $emissionUnit;
        }
        return $data;
    }

    /**
     * 获取清华排放量的公司的排口ID
     * @param $planNumber
     * @return array
     * @example [1,2,3]
     * @author hongwei 2019-07-16
     */
    public function getTsinghuaPlanDrainIds($planNumber)
    {
        $sql = 'select drain_id from control_plan_from_tsinghua_drain_details where plan_number = "' . $planNumber . '" group by drain_id order by drain_id asc';
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();

        //将drain_id取一维数组
        $newArr = [];
        foreach ($data as $k => $v) {
            array_push($newArr, $v[0]);
        }
        return $newArr;
    }

    /**
     * 获取清华排放量的公司的排口ID
     * @param $planNumber
     * @return array
     * @example [1,2,3]
     * @author hongwei 2019-07-16
     */
    public function getTsinghuaPlanCompanyIds($planNumber)
    {
        $sql = 'select company_id from control_plan_from_tsinghua_company_details where plan_number = "' . $planNumber . '" group by company_id order by company_id asc';
        $sth = $this->pdo->prepare($sql);
        $sth->execute();
        $data = $sth->fetchAll();

        //将company_id取一维数组
        $newArr = [];
        foreach ($data as $k => $v) {
            array_push($newArr, $v[0]);
        }
        return $newArr;
    }



    /**
     * +----------------------------------------------------------
     * 6.1 所有公司 - 所有排口 - 排放量
     * 清华排口方案排放量
     * +----------------------------------------------------------
     * @param string $type 脱硫或脱硝
     * @param $drainIds
     * @param $planNumber
     * @param $tsinghuaPlanName
     * @param string $emissionUnit 清华默认是 kg
     * +----------------------------------------------------------
     * @return array|mixed
     * @throws \Exception
     * @author hongwei 2019-06-29
     * +----------------------------------------------------------
     */
    public function getTsinghuaControlDrainIdsEmissionType($type, $drainIds, $planNumber, $tsinghuaPlanName, $emissionUnit = 'kg')
    {
        $field = '';
        if (strtoupper($type) == 'SO2') {
            $field = 'drain_id,drain_name,sum(drain_so2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'NO2') {
            $field = 'drain_id,drain_name,sum(drain_no2_emission) as tsinghua_emission';
        }
        if (strtoupper($type) == 'TSP') {
            $field = 'drain_id,drain_name,sum(drain_tsp_emission) as tsinghua_emission';
        }
        $sql  = 'select ' . $field . ' from control_plan_from_tsinghua_drain_details where control_mode_id = 1 and plan_number = "' . $planNumber .'"' ;
        $sql .= ' and tsinghua_plan_name = "' . $tsinghuaPlanName . '"' ;
        $sql .= ' and drain_id in ('.$drainIds.')';
        $sql .= ' group by drain_id order by drain_id asc';
        $data = $this->pdoFetchAll($sql);

        if (!$data) {
            exception('获取数据失败！');
        }
        //增加清华所要的排放量单位
        foreach ($data as $k => $v) {
            $data[$k]['tsinghua_emission']      = $v['tsinghua_emission'] * $this->getTsinghuaUnit($emissionUnit); //转换单位
            $data[$k]['tsinghua_emission_unit'] = $emissionUnit;
        }
        array_multisort(array_column($data, 'drain_id'), SORT_ASC, $data); //排口进行排序
        $data = field_as_key($data, 'drain_id');
        return $data;
    }
}